<extend name="Layout/ins_page" />
<block name="content">
	{:W('PageHeader/simple',array('name'=>'权限组管理'))}
	<form id="form_search" name="form_search" method="post" action="" >
		<div class="oprator-panel">
			<div class="col-10 x-select layui-input-inline">
				<select name="eq_pid" id="eq_pid" class="layui-input" lay-filter="pid" >
					<option value="#">全部</option>
					{:fill_option($group_list)}
				</select>
			</div><div class="split"></div>
			<a id="btn_save" class="btn">保存</a>
		</div>
	</form>
	<div class="sub-frame">
		<div class="sub-sidebar white">
			<table class="table table-bordered">
				<thead>
					<tr>
						<th>ID</th>
						<th>名称</th>
					</tr>
				</thead>
				<tbody>
					<volist name="list" id="data">
						<tr id="{$data.id}">
							<td>{$data.id}</td>
							<td>{$data.name}</td>
						</tr>
					</volist>
				</tbody>
			</table>
		</div>
		<div class="sub-content">
			<form id="form_data" method="post" action="">
				<input type="hidden" name="ajax" id="ajax" value="1">
				<input type="hidden" name="role_id" id="role_id">
				<input type="hidden" name="org_node_list" id="org_node_list">
				<input type="hidden" name="node_list" id="node_list">
				<div class="wrap tree_menu">
					<table class="table table-bordered">
						<tr>
							<th class="col-5 x-checkbox">
							<input class="toggle-select-all" type="checkbox" data="node_id[]" />
							</th>
							<th style="width:45%;text-align:center"> 菜单</th>
							<th style="width:15%;text-align:center"><span class="x-checkbox">
								<input class="toggle-select-all" type="checkbox" data="read[]" />
							</span>访问</th>
							<th style="width:15%;text-align:center"><span class="x-checkbox">
								<input class="toggle-select-all" type="checkbox" data="write[]" />
							</span> 编辑</th>
							<th style="width:15%;text-align:center"><span class="x-checkbox">
								<input class="toggle-select-all" type="checkbox" data="admin[]" />
							</span>管理</th>
						</tr>
						<volist name="node_list" id="data">
							<tr>
								<td class="col-5 x-checkbox text-center">
								<input  type="checkbox"  name="node_id[]"  value="{$data.id}" />
								</td>
								<td><span class="lbl level{$data.level}">&nbsp;{$data.name}</span></td>
								<if condition="$data['pid']!=0 && strpos($data['url'],'/index')||strpos($data['url'],'##')!==false">
									<td class="text-center"><span class="x-checkbox">
										<input  type="checkbox" name="read[]" value="{$data.id}" />
									</span></td>
									<td class="text-center"><span class="x-checkbox">
										<input  type="checkbox" name="write[]" value="{$data.id}"/>
									</span></td>
									<td class="text-center"><span class="x-checkbox">
										<input  type="checkbox" name="admin[]" value="{$data.id}"/>
									</span></td>
									<else/>
									<td class="text-center">&nbsp;</td>
									<td class="text-center">&nbsp;</td>
									<td class="text-center">&nbsp;</td>
								</if>
							</tr>
						</volist>
					</table>
				</div>
			</form>
		</div>
	</div>
</block>
<block name="js">
	<script type="text/javascript">
		layui.use(['global'], function() {
			var $ = layui.jquery;

			$('#eq_pid').on('change', function() {
				$('#form_search').submit();
			});

			set_return_url(null);
			set_val('eq_pid', '{$eq_pid}');

			$(".sub-sidebar tbody tr").click(function() {
				$(".sub-sidebar  tr").removeClass("active");
				$(this).attr("class", "active");
				$("#role_id").val($(this).attr("id"));
				send_ajax("{:U('get_node_list')}", "role_id=" + $(this).attr("id"), function(data) {
					showdata(data);
				});
				return false;
			});

			function showdata(result) {
				$(".tree_menu input").each(function() {
					$(this).prop("checked", false);
					//$(this).parents("td").removeClass("active");
				});
				for (var s in result.data) {
					$(".tree_menu input[name='node_id[]'][value=" + result.data[s].node_id + "]").prop("checked", "true");

					if (result.data[s].admin == 1) {
						$(".tree_menu input[name='admin[]'][value=" + result.data[s].node_id + "]").prop("checked", "true");
					}
					if (result.data[s].write == 1) {
						$(".tree_menu input[name='write[]'][value=" + result.data[s].node_id + "]").prop("checked", "true");
					}
					if (result.data[s].read == 1) {
						$(".tree_menu input[name='read[]'][value=" + result.data[s].node_id + "]").prop("checked", "true");
					}
				};
				$("#org_node_list").val('');
				$(".tree_menu input[name='node_id[]']:checked").each(function() {
					$("#org_node_list").val($("#org_node_list").val() + $(this).val() + ",");
				});
				$("#opmode").val("edit");
			};

			$("#btn_save").on('click', function() {
				if ($("#role_id").val() == '') {
					layer.msg('请选择权限组');
					return false;
				};
				$("#node_list").val('');
				$(".tree_menu input[name='node_id[]']:checked").each(function() {
					$("#node_list").val($("#node_list").val() + $(this).val() + ",");
				});
				send_form("form_data", "{:U('set_node')}", "{:U('node')}", function(ret) {
					if (ret.status) {
						layer.msg(ret.info, {
							time : 1200
						}, function() {
							location.reload(true);
						});
					}
				});
			});
		});

	</script>
</block>